using UnityEngine;
using UnityEditor;

namespace LCH
{
    /// <summary>
    /// 网格绘制器 - 事件处理功能模块
    /// </summary>
    public partial class LchMeshPainter
    {
        /// <summary>
        /// 更新场景视图窗口
        /// </summary>
        /// <param name="sceneView">场景视图</param>
        private void UpdateWindow(SceneView sceneView)
        {
            if (enable )
            {
                ctrlIsDown = Event.current.control;
                switch (panelType)
                {
                    case 0:
                        {
                            if (selectObject != null)
                            {
                                Selection.activeGameObject = selectObject.gameObject;
                                CheckMeshCollider();
                                PainterPanelUpdate(sceneView);
                                HandleKeyboard();
                            }
                            
                        }
                        break;
                    case 2:
                        {
                            //plantObject
                            Selection.activeGameObject = null;
                            CheckBuildTreeCollider();
                        }
                        break;
                }
                
            }
        }
        
        /// <summary>
        /// 更新绘制器面板
        /// </summary>
        /// <param name="sceneView">场景视图</param>
        private void PainterPanelUpdate(SceneView sceneView)
        {
            if (null == brush)
            {
                brush = new GameObject("Brush");
                brush.hideFlags = HideFlags.DontSaveInEditor | HideFlags.DontSaveInBuild | HideFlags.HideInHierarchy;
            }
            if (null != selectMeshCollider)
            {
                if (null != Event.current)
                {
                    UpdateBrush(sceneView);

                    if (Event.current.button == 0)
                    {
                        if (Event.current.type == EventType.MouseDown)
                        {
                            isDragging = true;
                            OnLeftMouseDown();
                            Event.current.Use();
                        }
                        else if (Event.current.type == EventType.MouseUp)
                        {
                            isDragging = false;
                            Event.current.Use();
                        }
                        else if (Event.current.type == EventType.MouseMove)
                        {
                            Event.current.Use();
                        }
                        else if (Event.current.type == EventType.MouseDrag)
                        {
                            OnLeftMouseDraging();
                            Event.current.Use();
                        }
                    }
                }
                else
                {
                    Debug.LogError("null event");
                }
            }
            else
            {
                //Debug.LogError("selectMeshCollider = null");
            }
        }
        
        /// <summary>
        /// 处理键盘快捷键
        /// </summary>
        void HandleKeyboard()
        {
            if (null != Event.current && Event.current.type == EventType.KeyUp  )
            {
                if (Event.current.keyCode == KeyCode.Z && Event.current.control)
                {
                    if (drawType == 0)
                    {
                        AddModify();
                        texturePaninnerUndoRedo.UnDo(editorCtrl1RT,editorFlowMapRT);
                    }
                    else
                    {
                        vertexUndoRedo.UnDo(selectObjectMesh);
                    }
                }
                else if (Event.current.keyCode == KeyCode.Y && Event.current.control)
                {
                    if (drawType == 0)
                    {
                        texturePaninnerUndoRedo.ReDo(editorCtrl1RT, editorFlowMapRT);
                    }
                    else
                    {
                        vertexUndoRedo.ReDo(selectObjectMesh);
                    }
                }
                else if (Event.current.keyCode == KeyCode.LeftBracket)
                {
                    brushStrength = Mathf.Max(brushStrength - 0.1f, 0);
                }

                else if (Event.current.keyCode == KeyCode.RightBracket)
                {
                    brushStrength = Mathf.Min(brushStrength + 0.1f, 1f);
                }
                else if (Event.current.keyCode == KeyCode.Minus)
                {
                    brushSize  =  Mathf.Max(brushSize - 1f,0);
                }

                else if (Event.current.keyCode == KeyCode.Equals)
                {
                    brushSize = brushSize  + 1;
                }
            }
        }
        
        /// <summary>
        /// 鼠标左键按下事件处理
        /// </summary>
        private void OnLeftMouseDown()
        {
            if (enable)
            {
                switch (panelType)
                {
                    case 0:
                        {
                            if (drawType == 0)
                            {
                                if (flowMapEditing)
                                {
                                    textureNeedPaint = false;
                                }
                                else
                                {
                                    textureNeedPaint = true;
                                }
                            }
                            else
                            {
                                vertexNeedPaint = true;
                            }

                        }
                        break;
                    case 2:
                        {
                            
                        }
                        break;
                }

            }
            
            
        }
        
        /// <summary>
        /// 鼠标左键拖拽事件处理
        /// </summary>
        private void OnLeftMouseDraging()
        {
            if (enable)
            {

                switch (panelType)
                {
                    case 0:
                        {
                            if (drawType == 0)
                            {
                               
                                textureNeedPaint = true;
                            }
                            else
                            {
                                vertexNeedPaint = true;
                            }

                        }
                        break;
                    case 2:
                        {
                            
                        }
                        break;
                }

            }
        }
    }
}

